package com.pengliu.service.impl;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pengliu.common.bean.OcrManage;
import com.pengliu.common.bean.OcrManageExample;
import com.pengliu.common.bean.OcrUser;
import com.pengliu.common.bean.OcrUserExample;
import com.pengliu.common.bean.SysUser;
import com.pengliu.common.exception.ServiceException;
import com.pengliu.config.BaseController;
import com.pengliu.dao.OcrManageMapper;
import com.pengliu.dao.OcrUserMapper;
import com.pengliu.dll.Ocr_dll;
import com.pengliu.service.OcrManageService;
import com.pengliu.util.Lists;

@Service
public class OcrManageServiceImpl implements OcrManageService{
	
	@Autowired
	private OcrUserMapper ocrUserMapper;
	
	@Autowired
	private OcrManageMapper ocrManageMapper;

	@Override
	public List<OcrUser> getOcrUsers(Integer sysUserId) {
		OcrUserExample example = new OcrUserExample();
		example.getCriteria().andSysUserIdEqualTo(sysUserId);
		List<OcrUser> list = ocrUserMapper.selectByExample(example);
		return list;
	}

	@Override
	public void addOcrUser(String userName, String passWord, Integer sysUserId) throws ServiceException {
		if(StringUtils.isBlank(userName)) {
			throw new ServiceException("用户名不能为空");
		}
		if(StringUtils.isBlank(passWord)) {
			throw new ServiceException("密码不能为空");
		}
		
		OcrUser ocrUser = new OcrUser();
		ocrUser.setUserName(userName);
		ocrUser.setPassWord(passWord);
		ocrUser.setSysUserId(sysUserId);
		ocrUser.setEnableState(1);
		ocrUser.setCreateTime(new Date());
		
		ocrUserMapper.insert(ocrUser);
	}

	@Override
	public void stopOcrUser(Integer id) {
		OcrUser ocrUser = ocrUserMapper.selectByPrimaryKey(id);
		ocrUser.setEnableState(0);
		ocrUserMapper.updateByPrimaryKeySelective(ocrUser);
	}

	@Override
	public void startOcrUser(Integer id) {
		OcrUser ocrUser = ocrUserMapper.selectByPrimaryKey(id);
		ocrUser.setEnableState(1);
		ocrUserMapper.updateByPrimaryKeySelective(ocrUser);
	}

	@Override
	public void delOcrUser(Integer id) {
		ocrUserMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PageInfo<OcrManage> getOcrManages(List<Integer> ocrUserIds, Integer pageNum,Integer pageSize) {
		//设置默认值
		pageNum = ObjectUtils.defaultIfNull(pageNum, 1);
		pageSize = ObjectUtils.defaultIfNull(pageSize, 10);
		
		PageHelper pageHelper = new PageHelper();
		pageHelper.startPage(pageNum, pageSize);
		
		OcrManageExample ocrManageExample = new OcrManageExample();
		com.pengliu.common.bean.OcrManageExample.Criteria criteria = ocrManageExample.getCriteria();
		if(!ocrUserIds.isEmpty()) {
			criteria.andOcrUserIdIn(ocrUserIds);
		}
		
		List<OcrManage> listOcrManage = ocrManageMapper.selectByExample(ocrManageExample);
		
		PageInfo<OcrManage> pageInfo = new PageInfo<OcrManage>(listOcrManage);
		
		
		
		return pageInfo;
	}

	@Override
	public OcrUser checkUser(String userName, String passWord) {
		OcrUserExample example = new OcrUserExample();
		example.getCriteria()
			.andUserNameEqualTo(userName)
			.andPassWordEqualTo(passWord);
		List<OcrUser> list = ocrUserMapper.selectByExample(example);
		
		OcrUser ocrUser = null;
		if(null != list && !list.isEmpty()) {
			ocrUser = list.get(list.size()-1);
		}
		return ocrUser;
	}

	@Override
	public OcrManage insertOcrManage(OcrManage ocrManage) {
		ocrManage.setCreateTime(new Date());
		ocrManageMapper.insert(ocrManage);
		return ocrManage;
	}

	@Override
	public void file_del(Long id) {
		ocrManageMapper.deleteByPrimaryKey(id);
	}
}
